import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/libback_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

@immutable
// ignore: must_be_immutable
class LibBackList extends StatefulWidget {
  String id;

  @override
  _LibBackListState createState() => _LibBackListState();

  LibBackList(this.id);
}

class _LibBackListState extends State<LibBackList> {
  List<dynamic> _data = List();

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    Provider.of<LibBackProvider>(context).getDetailListData(orderid: widget.id);
  }

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<LibBackProvider>(context).detailListData;
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "样品回库详情列表",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
        visibility: Provider.of<LibBackProvider>(context).netStatus2,
        builder: Column(
          children: <Widget>[
            Expanded(
                child: RefreshIndicator(
                    child: ListView.separated(
                        itemBuilder: (BuildContext context, int index) {
                          return TextCartItem(
                            data: _data,
                            index: index,
                            onTapFuns: (int index) {
                              Navigator.of(context)
                                  .pushNamed("/lib_back_details",
                                      arguments: _data[index])
                                  .then((data) {
                                if (data == "ok") {
                                  setState(() {
                                    Provider.of<LibBackProvider>(context)
                                        .getDetailListData(orderid: widget.id);
                                  });
                                }
                              });
                            },
                          );
                        },
                        separatorBuilder: (BuildContext context, int index) {
                          return Divider(color: Colors.black26);
                        },
                        itemCount: _data.length),
                    onRefresh: _refresh)),
          ],
        ),
      ),
    );
  }
}

class _Text extends Text {
  TextStyle style;

  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
  Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
        data[index]['ispickup'] == true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("物资名称:${data[index]['materialname']}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("回库库区:${data[index]['areaname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("回库货架:${data[index]['shelfname'] ?? "暂无"}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("回库货位:${data[index]['locationname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: RichText(
                    text: TextSpan(
                      text: '抽样数量:',
                      style:
                          TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data[index]['quantity'].toString(),
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
